package org.lex.game.sudoku.view;

import java.text.SimpleDateFormat;
import java.util.Comparator;

import org.lex.game.sudoku.model.Point;

public class Operation {
	public static final String TIME_FORMAT = "HH:mm:ss.S";

	private final long when;
	private final Point where;
	private final int oldValue;
	private final int newValue;

	public Operation(long when, Point where, int oldValue, int newValue) {
		this.when = when;
		this.where = where;
		this.oldValue = oldValue;
		this.newValue = newValue;
	}

	public long getWhen() {
		return when;
	}

	public Point getWhere() {
		return where;
	}

	public int getOldValue() {
		return oldValue;
	}

	public int getNewValue() {
		return newValue;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + newValue;
		result = prime * result + oldValue;
		result = prime * result + (int) (when ^ (when >>> 32));
		result = prime * result + ((where == null) ? 0 : where.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Operation other = (Operation) obj;
		if (newValue != other.newValue)
			return false;
		if (oldValue != other.oldValue)
			return false;
		if (when != other.when)
			return false;
		if (where == null) {
			if (other.where != null)
				return false;
		} else if (!where.equals(other.where))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "HistoryOperation [when=" + new SimpleDateFormat(TIME_FORMAT).format(this.when) + ", where=" + where
				+ ", oldValue=" + oldValue + ", newValue=" + newValue + "]";
	}

	public static final class TimeComparator implements Comparator<Operation> {

		@Override
		public int compare(Operation o1, Operation o2) {
			return Long.compare(o1.when, o2.when);
		}

	}
}
